
package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import java.awt.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import tasks.CreateTorrent;

public class FenetreCreate extends JFrame{
    
    
    private JLabel pathLabel;
    private JTextField path; 
    private JButton browse;
    private JFileChooser fc;
    private JLabel tracker;
    private JTextField trackerURL;
    private JButton ok;
    private JPanel pan;
    private JLabel authorLabel;
    private JTextField author;
    private JLabel commentLabel;
    private JTextArea comment;
    private FenetrePrincipale fenetrePrincipale;
    
    public FenetreCreate() {
    	this(null);
    }
    public FenetreCreate(FenetrePrincipale fenetrePrincipale) {
        
        this.pathLabel = new JLabel("Fichier : ", SwingConstants.RIGHT);
        this.path = new JTextField(29);
        this.browse = new JButton("Parcourir..");
        this.fc = new JFileChooser();
        this.tracker = new JLabel("Tracker : ", SwingConstants.RIGHT);
        this.trackerURL = new JTextField(35);
        this.authorLabel = new JLabel("Auteur : ", SwingConstants.RIGHT);
        this.author = new JTextField(35);
        this.author.setText("BBQTorrent ver. 0.000000001");
        this.ok = new JButton("OK");
        this.pan = new JPanel();
        this.commentLabel = new JLabel("Commentaire : ", SwingConstants.RIGHT);
        this.comment = new JTextArea(2, 35);
        this.fenetrePrincipale = fenetrePrincipale;
        
        this.comment.setWrapStyleWord(true);
        this.comment.setLineWrap(true);
        JScrollPane scroll = new JScrollPane(this.comment);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        this.pan.setLayout(new BorderLayout());
        

        this.browse.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                fc.setDialogTitle("Parcourir..");
                fc.setApproveButtonText("Ajouter");
                fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int returnVal = fc.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {   
                    File choosen = fc.getSelectedFile();
                    path.setText(choosen.getPath());
                }
            }
        });
        
        this.ok.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            	if (!path.getText().isEmpty()){
            		File t = new File(path.getText());
            		if (!tracker.getText().isEmpty()){
            			CreateTorrent ct = new CreateTorrent(t,32768, trackerURL.getText(),author.getText(), comment.getText());
            			ct.run();
            		}
            	}
                
            	FenetreCreate.this.fenetrePrincipale.getCreate().setEnabled(true);
            	
                dispose();
            }
        });
        
        JPanel panleft = new JPanel(new GridLayout(4,1));
        panleft.add(pathLabel);
        panleft.add(tracker);
        panleft.add(authorLabel);
        panleft.add(commentLabel);
        
        JPanel pancent = new JPanel(new GridLayout(4,1));
        JPanel pan1 = new JPanel();
        pan1.add(path);
        pan1.add(browse);
        pancent.add(pan1);
        JPanel pan2 = new JPanel();
        pan2.add(trackerURL);
        pancent.add(pan2);
        JPanel pan3 = new JPanel();
        pan3.add(author);
        pancent.add(pan3);
        JPanel pan4 = new JPanel();
        pan4.add(scroll);
        pancent.add(pan4);
        
        JPanel panok = new JPanel();
        ok.setSize(new Dimension(40,20));
        panok.add(ok);
        panok.setSize(200, 10);
        
        
        this.pan.add(panleft, BorderLayout.WEST);
        this.pan.add(pancent, BorderLayout.CENTER);
        this.pan.add(panok, BorderLayout.SOUTH);
        
        
        this.add(pan);       
        this.setSize(600, 270);
        this.setResizable(false);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.setTitle("Créer");
    }

	

}